import tools from '@/tools'

import {
	login,
	logout
} from '@/api/common.api'
import {
	getUserInfoRole
} from "@/api/permission.api"
import {
	getUserInfoByToken,
	getRole
} from '@/api/mall.api'
import {
	Base64
} from "js-base64";

export default {
	state: {
		// userRoleList: tools.localRead('userRoleList') ? tools.localRead('userRoleList').split(',') : [],
		access_token: tools.localRead('access_token') ? tools.localRead('access_token') : '',
		token_type: tools.localRead('token_type') ? tools.localRead('token_type') : '',
		refresh_token: tools.localRead('refresh_token') ? tools.localRead('refresh_token') : '',
		userId: tools.localRead('userId') ? tools.localRead('userId') : '',
		jti: tools.localRead('jti') ? tools.localRead('jti') : '',
		user_name: tools.localRead('user_name') ? tools.localRead('user_name') : '',
		authorities: tools.localRead('authorities') ? tools.localRead('authorities') : '',
		user_token: tools.localRead('user_token') ? tools.localRead('user_token') : ''
	},
	getters: {},
	mutations: {
		set_access_token(state, access_token = '') {
			tools.localSave('access_token', access_token)
			state.access_token = access_token
		},
		set_token_type(state, token_type = '') {
			tools.localSave('token_type', token_type)
			state.token_type = token_type
		},
		set_refresh_token(state, refresh_token = '') {
			tools.localSave('refresh_token', refresh_token)
			state.refresh_token = refresh_token
		},
		set_userId(state, userId = '') {
			tools.localSave('userId', userId)
			state.userId = userId
		},
		set_jti(state, jti = '') {
			tools.localSave('jti', jti)
			state.jti = jti
		},
		set_user_name(state, user_name = '') {
			tools.localSave('user_name', user_name)
			state.user_name = user_name
		},
		set_authorities(state, authorities = []) {
			tools.localSave('authorities', authorities.join(','))
			state.authorities = authorities

		},
		set_userToken(state, user_token = '') {
			tools.localSave('user_token', user_token)
			state.user_token = user_token
		},
		set_permissions(state, permissions = []) {
			tools.localSave('permissions', permissions)
			state.permissions = permissions
		}
		// setUserRoleList (state, list = []) {
		//   tools.localSave('userRoleList', list.join(','))
		//   state.userRoleList = list
		// },
	},
	actions: {
		// 登录接口
		handleLogin({
			commit
		}, {
			username,
			password,
			verifyCode,
			Uuid
		}) {
			const strAccount = tools.trim(username)
			const strPasswd = tools.trim(password)
			const strVerifyCode = tools.trim(verifyCode)
	
			return new Promise((resolve, reject) => {
				getUserInfoByToken({
					account:localStorage.getItem("account")
				}).then((res)=>{
					console.log(res);
					console.log(1111111111);
					// localStorage.setItem('userId', JSON.stringify(res.id));
					var realName = res.name;
					localStorage.setItem("user_name", realName);
					localStorage.setItem("userId", res.id);
					localStorage.setItem("mobile", res.mobile);
					resolve(res)
				})
	
			})
		},
		handleLogout() {
			return new Promise((resolve, reject) => {
				logout().then(res => {
					if (res.resultCode === 200) {
						localStorage.clear()
						resolve()
					} else {
						reject(res.resultMsg)
					}
				}).catch(err => {
					reject(err)
				})
			})
		}
	}
}